package com.shelpe.serivces.conf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/confservice")
@Slf4j
public class MonitorController {

	@Autowired
	private CacheManager cacheManaer;
	
	@RequestMapping(path = "/oschina/hook", method = RequestMethod.POST)
	@ResponseBody
	public void oschinaHook(@RequestBody PushHook hook){
		if(log.isDebugEnabled()){
			log.debug("hook: {}", hook);
		}
		this.cacheManaer.getCache("git").clear();
	}
	
	@RequestMapping(path = "/refresh", method = RequestMethod.GET)
	@ResponseBody
	public void refresh(){
		this.cacheManaer.getCache("git").clear();
	}
	
	@Data
	public static class PushHook{
		private String password;
		@JsonProperty("hook_name")
		private String hookName;
		@JsonProperty("push_data")
		private PushData pushData;
	}
	
	@Data
	public static class PushData{
		private Repository repository;
	}
	
	@Data
	public static class Repository{
		private String name;
		private String url;
		private String description;
		private String homepage;
	}
}
